import os

from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp import template

import datamodel
from globals import Globals

class Course(webapp.RequestHandler):
    def get(self):
        if self.request.get('ckey'):
            course = db.get(db.Key(self.request.get('ckey')))
            dept = course.dept
            
            courseName = (course.cName + ' (' + dept.dAbbr + ' ' + str(course.cNo) + ')')
            
            wall = self.getWall(course, courseName)
            
            template_values = {
                               'globals': Globals(),
                               'header': courseName,
                               'wall': wall}
            
            path = os.path.join(os.path.dirname(__file__), 'templates/course.html')
            self.response.out.write(template.render(path, template_values))
        else:
            self.redirect('/')
            
    def getWall(self, course, courseName):
        wall = '<div id="wall">\n'
        wall += '<form method="post" action="/process">\n'
        wall += '<textarea name="text"></textarea>\n'
        wall += '<input type="submit" value="Post" />\n'
        wall += '<input type="hidden" name="nsfn" value="wallpost" />\n'
        wall += '<input type="hidden" name="ckey" value="'+self.request.get('ckey')+'" />\n'
        wall += '<input type="hidden" name="redirect" value="/course?ckey='+self.request.get('ckey')+'" />\n'
        wall += '</form>'
        
        posts = course.wall_posts
        
        if posts.count() > 0:
            wall += '<ul>'
            for post in posts:
                wall += '<li><span class="user"><span class="user">' + post.user.nickname() + '<span class="user"></span> ' + post.text
                wall += self.getComments(post)
                wall += '<div class="comment" id="'+str(post.key())+'">\n'
                wall += '<form method="post" action="/process">\n'
                wall += '<textarea name="text"></textarea>\n'
                wall += '<input type="submit" value="Comment" />'
                wall += '<input type="hidden" name="nsfn" value="wallpost" />\n'
                wall += '<input type="hidden" name="comment_of" value="'+str(post.key())+'" />\n'
                wall += '<input type="hidden" name="redirect" value="/course?ckey='+self.request.get('ckey')+'" />\n'
                wall += '</form>\n'
                wall += '</div>\n'
                wall += '</li>'
        else:
            wall += '<p>Be the first to discuss ' + courseName + '</p>'
        
        wall += '</div>'
        
        return wall
    
    def getComments(self, post):
        comments = post.comments
        
        if comments.count() > 0:
            wall = '<ul>\n'
            for comment in comments:
                wall += '<li><span class="user">' + comment.user.nickname() + '</span> ' + comment.text
                wall += self.getComments(comment)
                wall += '</li>'
            wall += '</ul>\n'
        else:
            wall = ''
            
        return wall